package com.zhp.fs.utils;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Locale;
import java.util.Map;

/**
 * 时间：2017/6/14  12:41
 * 作者 ：傅文江
 */
public class RequestUtils {

    public static void doPrint(HttpServletRequest request) {
        StringBuilder sb = new StringBuilder();
        Map<String, String[]> parameterMap = request.getParameterMap();
        if (null != parameterMap) {
            for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) {
                sb.append(entry.getKey()).append("=").append(Arrays.toString(entry.getValue()));
                sb.append("  ");
            }
        }
        String userAgent = request.getHeader("user-agent");
        LogUtils.error("========================请求开始========================");
        LogUtils.error("请求时间：" + DateUtils.getNowTimeString());
        LogUtils.error("请求地址：" + request.getRequestURL());
        LogUtils.error("请求方法：" + request.getMethod());
        LogUtils.error("请求参数：" + sb.toString());
        LogUtils.error("请求操作系统：" + getOS(userAgent));
        LogUtils.error("请求语言环境：" + getLocale(request.getLocale()));
        LogUtils.error("请求浏览器：" + getNavigator(userAgent));
        LogUtils.error("请求客户机IP地址：" + request.getRemoteAddr());
        LogUtils.error("请求客户机主机名：" + request.getRemoteHost());
        LogUtils.error("请求客户机用户：" + request.getRemoteUser());
        LogUtils.error("请求客户机端口号：" + request.getRemotePort());
        LogUtils.error("========================请求结束========================");
    }


    /**
     * @param locale 环境
     * @return 语言环境名称
     */
    private static String getLocale(Locale locale) {
        if (null == locale) {
            return null;
        }
        if (Locale.SIMPLIFIED_CHINESE.equals(locale)) return "简体中文";
        if (Locale.TRADITIONAL_CHINESE.equals(locale)) return "繁体中文";
        if (Locale.ENGLISH.equals(locale)) return "英文";
        if (Locale.JAPANESE.equals(locale)) return "日文";
        return "未知语言环境" + locale.toString();
    }

    /**
     * @param userAgent 用户代理信息
     * @return 客户端浏览器信息
     */
    private static String getNavigator(String userAgent) {
        if (StringUtils.isNullOrEmpty(userAgent)) {
            return null;
        }
        if (userAgent.indexOf("Edge") > 0) return "Edge 浏览器";
        if (userAgent.indexOf("TencentTraveler") > 0) return "腾讯浏览器";
        if (userAgent.indexOf("Maxthon") > 0) return "Maxthon浏览器";
        if (userAgent.indexOf("MyIE2") > 0) return "MyIE2浏览器";
        if (userAgent.indexOf("Firefox") > 0) return "Firefox浏览器";
        if (userAgent.indexOf("MSIE") > 0) return "IE 浏览器";
        if (userAgent.indexOf("Chrome") > 0) return "Chrome 浏览器";
        return "未知浏览器" + userAgent;
    }

    /**
     * @param userAgent 用户代理信息
     * @return 客户端操作系统
     */
    private static String getOS(String userAgent) {
        if (StringUtils.isNullOrEmpty(userAgent)) {
            return null;
        }
        if (userAgent.indexOf("Windows NT 5.1") > 0) return "Windows XP";
        if (userAgent.indexOf("Windows 98") > 0) return "Windows 98";
        if (userAgent.indexOf("Windows NT 5.0") > 0) return "Windows 2000";
        if (userAgent.indexOf("Windows NT 10.0") > 0) return "Windows 10";
        if (userAgent.indexOf("Linux") > 0) return "Linux";
        if (userAgent.indexOf("Unix") > 0) return "Unix";
        if (userAgent.indexOf("Chrome") > 0) return "Chrome";
        return "未知" + userAgent;
    }
}
